#!/usr/bin/env python
"""

    $itemname$.py
    [--log_file PATH]
    [--verbose]

"""

################################################################################
#
#   $itemname$
#
#
#   Copyright (c) $localdate$ Leo Goodstadt
#
#   Permission is hereby granted, free of charge, to any person obtaining a copy
#   of this software and associated documentation files (the "Software"), to deal
#   in the Software without restriction, including without limitation the rights
#   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the Software is
#   furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#   THE SOFTWARE.
#################################################################################

import sys, os

# add self to search path for testing
if __name__ == '__main__':
    exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    module_name = os.path.split(sys.argv[0])[1]
    module_name = os.path.splitext(module_name)[0];
else:
    module_name = __name__

# Use import path from ~/python_modules
if __name__ == '__main__':
    sys.path.append("/home/lg/python_modules")



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   options


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


if __name__ == '__main__':
    import argparse
    from common_parser import get_common_pipeline_parser

    parser = argparse.ArgumentParser(description='WHAT DOES THIS DO?',
                                    #formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                    parents=[get_common_pipeline_parser()],
                                     )
    required_options = argparse._ArgumentGroup(parser, 'mandatory arguments')
    parser._action_groups.insert(0, required_options)

    required_options.add_argument('-i', '--input_file', nargs='?', type=argparse.FileType('r'),
                                   default=sys.stdin)

    #
    #   Multiple input files
    #
    #required_options.add_argument("-i", "--input_file", dest="input_files", required = True,
    #                                metavar="FILE",
    #                                type= str,
    #                                action="append",
    #                                help="Name and path of input file.")
    #parser.set_defaults(input_files     = [["some_directory/*"
    #                                    ]],
    #                    )




    options = parser.parse_args()


    if not options.log_file:
        options.log_file            = os.path.join("$itemname$.log")


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   imports


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

from ruffus import *
from ruffus.ruffus_exceptions import JobSignalledBreak

#from json import dumps
#from collections import defaultdict



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Functions


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Logger


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

from lg_program_logging import MESSAGE, getLogger

if __name__ == '__main__':

    #
    #   set up log
    #
    logger = getLogger(module_name, options.log_file, options.verbose)


    #
    #   log programme parameters
    #
    if not options.skip_parameter_logging:
        logger.info(" ".join(sys.argv))



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Pipeline


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Main logic


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
if __name__ == '__main__':
#   debug code not run if called as a module
#
    if options.debug:
    #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    #   Testing


    #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

        import unittest
        class Test_$itemname$(unittest.TestCase):

            #       self.assertEqual(self.seq, range(10))
            #       self.assert_(element in self.seq)
            #       self.assertRaises(ValueError, random.sample, self.seq, 20)



            def test_function(self):
                """
                    test
                """

        #
        #   call unit test without parameters
        #

        if sys.argv.count("--debug"):
            sys.argv.remove("--debug")
        unittest.main()

    else:

        input_file = open(options.input_file)
        for cnt_lines, line in enumerate(input_file):
            if not len(line) || line[0] == '#':
                continue



        #
        #   Multiple input files with glob specifications
        #
        import glob

        input_files = []
        for file_group in options.input_files:
            for input_glob in file_group:
                print input_glob
                print glob.glob(input_glob)
                input_files.extend(glob.glob(input_glob))

        for input_file_name in input_files:
            input_file = open(input_file_name)
            for cnt_lines, line in enumerate(input_file):
                if not len(line) || line[0] == '#':
                    continue





        if options.just_print:
            pipeline_printout(sys.stdout, options.target_tasks, options.forced_tasks,
                                verbose=options.verbose)

        elif options.flowchart:
            pipeline_printout_graph (     open(options.flowchart, "w"),
                                 options.flowchart_format,
                                 options.target_tasks,
                                 options.forced_tasks,
                                 draw_vertically = not options.draw_horizontally,
                                 no_key_legend   = not options.key_legend_in_graph)
        else:
            pipeline_run(options.target_tasks, options.forced_tasks,
                                multiprocess    = options.jobs,
                                logger          = stderr_logger,
                                verbose         = options.verbose)








